from transformers import AutoModelForSequenceClassification,AutoTokenizer,pipeline
import torch
#文本分类
#加载模型
model = AutoModelForSequenceClassification.from_pretrained("D:\models\multilingual-sentiment-analysis")
#创建分词器
tokenizer = AutoTokenizer.from_pretrained("D:\models\multilingual-sentiment-analysis")

# #创建pipeline
# pipe = pipeline('text-classification', model=model, tokenizer=tokenizer)
# res = pipe("我今天很高兴")
# print(res)

input_text = "我很高兴"
#pt表示pytorch的张量对象
inputs = tokenizer(input_text, return_tensors="pt")
print(inputs)
res = model(**inputs)
print(res)
logits = torch.softmax(res.logits, dim=-1)
print(logits)
pred = torch.argmax(logits).item()
print(pred)
print(model.config.id2label.get(pred))